package org.keshe.controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.keshe.CurrentUser;
import org.keshe.data.BedData;
import org.keshe.data.BedDataWithButton;
import org.keshe.user.AdminUser;

import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class InStu implements Initializable {

    private AdminUser adminUser;

    @FXML
    private TextField inputSid;

    @FXML
    private TableView<BedDataWithButton> table;

    @FXML
    private TableColumn<BedDataWithButton, String> bedName;

    @FXML
    private TableColumn<BedDataWithButton, String> dormName;

    @FXML
    private TableColumn<BedDataWithButton, String> floor;

    @FXML
    private TableColumn<BedDataWithButton, String> buildingName;

    @FXML
    public TableColumn<BedDataWithButton, Button> buttonCol;

    @FXML
    public void resetClick(ActionEvent event) {
        table.getItems().clear();
        inputSid.setText("");
        inputSid.setEditable(true);
    }

    @FXML
    void searchClick(ActionEvent event) {
        inputSid.setEditable(false);
        if(inputSid.getText().isEmpty()){
            emptyFail.show();
            resetClick(event);
            return;
        }
        String s_id = inputSid.getText();
        try {
            adminUser.getStuDataById(s_id);
        } catch (SQLException e) {
            getFail.show();
            e.printStackTrace();
            resetClick(event);
            return;
        }
        try {
            if(adminUser.isOccupyBed(s_id)){
                occupied.show();
                resetClick(event);
            }else{
                List<BedData> list = adminUser.searchSpareBedForStu(adminUser.getStuDataById(s_id));
                loadTableContent(list);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
    Alert checkinSucc, checkinFail, occupied,emptyFail, getFail;
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        if(CurrentUser.USER instanceof AdminUser){
            adminUser = (AdminUser) CurrentUser.USER;
        }else{
            throw new RuntimeException();
        }
        bedName.setCellValueFactory(new PropertyValueFactory<>("bedName"));
        dormName.setCellValueFactory(new PropertyValueFactory<>("dormName"));
        floor.setCellValueFactory(new PropertyValueFactory<>("floor"));
        buildingName.setCellValueFactory(new PropertyValueFactory<>("buildingName"));
        buttonCol.setCellValueFactory(new PropertyValueFactory<>("button"));

        checkinFail = new Alert(Alert.AlertType.ERROR, "操作失败，数据或已改变", ButtonType.CLOSE);
        checkinSucc = new Alert(Alert.AlertType.INFORMATION, "操作成功", ButtonType.OK);
        emptyFail = new Alert(Alert.AlertType.WARNING, "学号不能为空", ButtonType.OK);
        getFail = new Alert(Alert.AlertType.WARNING, "查无此人", ButtonType.OK);
        occupied = new Alert(Alert.AlertType.WARNING, "已有床位", ButtonType.OK);

    }

    private void loadTableContent(List<BedData> list){
        ObservableList<BedDataWithButton> observableList =
                FXCollections.observableArrayList();
        for(BedData bedData : list){
            observableList.add(new BedDataWithButton(bedData));
        }
        table.setItems(observableList);
        for(BedDataWithButton bedDataWithButton : table.getItems()){
            bedDataWithButton.getButton().setText("入住");
            bedDataWithButton.getButton().setOnAction(e -> {
                try {
                    adminUser.checkInStu(bedDataWithButton.getBedName(), inputSid.getText());
                    checkinSucc.show();

                } catch (SQLException ex) {
                    checkinFail.show();
                    ex.printStackTrace();
                }finally {
                    resetClick(e);
                }
            });
        }
    }


}
